<?php

namespace app\zc\controller;

use cmf\controller\AdminBaseController;
use app\zc\validate\NewsValidate;
use app\zc\model\NewsModel;

class NewsController extends AdminBaseController
{
    public function index()
    {
        // 获取搜索关键字
        $keyword = input('get.keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['title|author', 'like', '%' . $keyword . '%'];
        }

        // 查询所有新闻并传递给视图
        $newsList = NewsModel::where($where)
            ->order("list_order", 'desc')
            ->paginate(3);

        foreach ($newsList as $news) {
            $content = $news['content'];
            $content1 = htmlspecialchars_decode($content);
            $content2 = cmf_replace_content_file_url($content1, true);
            $news['content'] = $content2;
        }

        $page = $newsList->render();

        $this->assign('newsList', $newsList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function content($id)
    {
        $article = NewsModel::find($id);
        if (!$article) {
            $this->error('文章不存在');
        }

        $content = $article['content'];
        $content1 = htmlspecialchars_decode($content);
        $content2 = cmf_replace_content_file_url($content1, true);
        $article['content'] = $content2;

        $this->assign('article', $article);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addpost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new NewsValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            // 处理内容中的图片地址
            $content = $data['content'];
            $content1 = htmlspecialchars_decode($content);
            $content2 = cmf_replace_content_file_url($content1, true);
            $content3 = htmlspecialchars($content2);
            $data['content'] = $content3;

            // 确保 `switch` 字段有默认值
            $data['switch'] = isset($data['switch']) ? $data['switch'] : 1;

            $news = new NewsModel();
            $news->title = $data['title'];
            $news->image = $data['image'];
            $news->look_num = $data['look_num'];
            $news->author = $data['author'];
            $news->abstract = $data['abstract'];
            $news->content = $data['content'];
            $news->switch = $data['switch'];

            if ($news->save()) {
                $this->success('添加成功', url('News/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function changeStatus()
    {
        $id = input('param.id', 0, 'intval'); // 获取传入的新闻 ID
        $news = NewsModel::find($id);

        if ($news) {
            // 切换状态
            $news->switch = $news->switch == 1 ? 0 : 1;

            if ($news->save()) {
                $this->success('状态切换成功', 'News/index'); // 成功后重定向到新闻列表页
            } else {
                $this->error('状态切换失败', 'News/index'); // 失败后也重定向到新闻列表页
            }
        } else {
            $this->error('新闻不存在', 'News/index'); // 新闻不存在时重定向到新闻列表页
        }
    }



    public function edit($id)
    {
        $news = NewsModel::find($id);
        if (!$news) {
            $this->error('新闻不存在');
        }

            $content = $news['content'];
            $content1 = htmlspecialchars_decode($content);
            $content2 = cmf_replace_content_file_url($content1, true);
            $news['content'] = $content2;

        $this->assign('news', $news);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new NewsValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $news = NewsModel::find($data['id']);
            if (!$news) {
                $this->error('新闻不存在');
            }

            // 处理内容中的图片地址
            $content = $data['content'];
            $content1 = htmlspecialchars_decode($content);
            $content2 = cmf_replace_content_file_url($content1, true);
            $content3 = htmlspecialchars($content2);
            $data['content'] = $content3;

            //switch的默认值
            $data['switch'] = isset($data['switch']) ? $data['switch'] : 1;

            $news->title = $data['title'];
            $news->image = $data['image'];
            $news->look_num = $data['look_num'];
            $news->author = $data['author'];
            $news->abstract = $data['abstract'];
            $news->content = $data['content'];
            $news->switch = $data['switch'];
            $news->update_time = date('Y-m-d H:i:s');
            $news->delete_time = date('Y-m-d H:i:s');

            if ($news->save()) {
                $this->success('编辑成功', url('News/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }

    public function delete($id)
    {
        $news = NewsModel::find($id);
        if (!$news) {
            $this->error('新闻不存在');
        }

        if ($news->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new NewsModel());
        $this->success('排序成功');
    }
}
